package org.poem.fullhandler;

import com.zaxxer.hikari.HikariDataSource;
import org.poem.ContextDatabase;
import org.poem.DataSourceDriverHelper;
import org.poem.DataSourceUrlHelper;
import org.poem.ExecTaskDetailPlanVO;
import org.poem.enums.EnumDataType;
import org.poem.exec.annotation.CanalHandle;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author poem
 */
@Service
@CanalHandle
public class MSSClientFullHandler extends DatabasesClientFullHandler {

    public static void main(String[] args) throws Exception {
        MSSClientFullHandler mssClientFullHandler = new  MSSClientFullHandler();
        ExecTaskDetailPlanVO execTaskDetailPlanVO = new ExecTaskDetailPlanVO();
        execTaskDetailPlanVO.setEnumDataType(EnumDataType.SQLSERVER);
        execTaskDetailPlanVO.setName("");
        execTaskDetailPlanVO.setSourceIp("47.108.144.111");
        execTaskDetailPlanVO.setSourcePort(1433);
        execTaskDetailPlanVO.setSourcePasswd("R$v3zh#KRxgI0A0^XVRF");
        execTaskDetailPlanVO.setSourceSchema("CDXY_YD_PREDATA_PRODUCTION");
        execTaskDetailPlanVO.setSourceTableName("car_consign_dispatch_order");
        execTaskDetailPlanVO.setSourceUserName("CDXY_YD_PREDATA_PRODUCTION_READ");

        execTaskDetailPlanVO.setTargetTable(execTaskDetailPlanVO.getSourceTableName());
        mssClientFullHandler.handle(execTaskDetailPlanVO);
    }
    /**
     * 任务执行
     */
    private static final ConcurrentHashMap<String, JdbcTemplate> JDBC_TEMPLATECON_CURRENT_HASHMAP = new ConcurrentHashMap<>();

    /**
     * 初始化链接
     *
     * @param url
     * @param password
     * @param userName
     * @return
     */
    private static JdbcTemplate initJdbcTemplate(String url, String password, String userName) {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName(DataSourceDriverHelper.getJdbc(url));
        dataSource.setJdbcUrl(url);
        dataSource.setUsername(userName);
        dataSource.setPassword(password);
        dataSource.addDataSourceProperty("cachePrepStmts", "true");
        dataSource.addDataSourceProperty("prepStmtCacheSize", "500");
        dataSource.addDataSourceProperty("prepStmtCacheSqlLimit", "5000");
        dataSource.addDataSourceProperty("sendStringParametersAsUnicode","false");
        dataSource.setIdleTimeout(300000);
        dataSource.setValidationTimeout(60000);
        dataSource.setConnectionTestQuery("select 1");
        dataSource.setMaxLifetime(600000);
        return new JdbcTemplate(dataSource);
    }

    /**
     *
     * @param execTaskDetailPlanVO
     */
    @Override
    protected void setAppSchema(ExecTaskDetailPlanVO execTaskDetailPlanVO) {
        ContextDatabase.setAppSchema(EnumDataType.SQLSERVER);
    }


    /**
     *
     * @param execTaskDetailPlanVO
     * @return
     */
    @Override
    protected JdbcTemplate getSourceJdbcTemplate(ExecTaskDetailPlanVO execTaskDetailPlanVO) {
        String url = DataSourceUrlHelper.getDatabaseUrl(execTaskDetailPlanVO.getSourceIp(),
                execTaskDetailPlanVO.getSourcePort(), execTaskDetailPlanVO.getSourceSchema(), "8");
        JdbcTemplate jdbcTemplate = JDBC_TEMPLATECON_CURRENT_HASHMAP.get(url);
        if (jdbcTemplate == null) {
            jdbcTemplate = initJdbcTemplate(url, execTaskDetailPlanVO.getSourcePasswd(), execTaskDetailPlanVO.getSourceUserName());
            JDBC_TEMPLATECON_CURRENT_HASHMAP.put(url, jdbcTemplate);
        }
        return jdbcTemplate;
    }

    @Override
    protected JdbcTemplate getAppJdbcTemplate(ExecTaskDetailPlanVO execTaskDetailPlanVO) {
        return null;
    }
}
